import logging

from celery_tasks.main import app
from meiduo_mall.libs.yuntongxun.sms import CCP

logger = logging.getLogger('django')
# 验证码短信模板
SMS_CODE_TEMP_ID = 1


@app.task(name='send_sms_code')
def send_sms_code(mobile, code, expires):
    # 发送验证码
    result = 1
    try:
        ccp = CCP()
        result = ccp.send_template_sms(mobile, [code, expires], SMS_CODE_TEMP_ID)
    except Exception as e:
        logger.error("发送验证码短信[异常][ mobile: {}, message: {} ]".format(mobile, e))
    else:
        if result != 0:
            logger.warning("发送验证码短信[异常][ mobile: {} ]".format(mobile))
        else:
            logger.info("发送验证码短信[正常][ mobile: {} ]".format(mobile))
    return result

